$(function () {
    var verify_url = $('#js-code').attr('src');
    $("#js-code").on("click",function(){
        this.src = verify_url + '?tt=' + Math.random();
    });
    $("#js-reservation").on("submit",function(){
        if(checkRequire(this)){
            var _this = this;
            $.post(this.action,$(this).serialize(),function(data){
                alert(data.info);
//                $.fancybox('<div class="pop-msg">'+data.info+'</div>');
                if(data.status===1){
                    $("input,textarea",_this).val("");
                }
            },"json");
        }
        return false;
    });

    //以下是placehoder
    if(!hasPlaceholderSupport()){
        $('input[placeholder],textarea[placeholder]').each(function () {
            var $o=$(this),v=$o.val(),p=$o.attr('placeholder');
            if(v===''){$o.val(p);}
            $o.focus(function(){
                if(this.value===p){this.value="";}
            }).blur(function() {
                if(this.value===''){this.value=p;}
            });
        });
    }
});
/*检查是否支持placeholder*/
function hasPlaceholderSupport() {
    var input = document.createElement('input');
    return ('placeholder' in input);
}
/*邮箱验证*/
function isEmail(str){
    var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    return reg.test(str);
}
function isMobile(str){
    var reg = /^1\d{10}$/;
    return reg.test(str);
}
/*检查必填项*/
function checkRequire(form){
    var flag = true;
    $(':text.required', form).each(function(){
        var v = $.trim(this.value);
        if(v === '' || v === $(this).attr("placeholder")){
            flag = false;
            $(this).addClass("error");
            alert("请填写必填项！");
            return false;
        }
        if($(this).hasClass("is-email") && !isEmail(v)){
            flag = false;
            $(this).addClass("error");
            alert("请填写正确的邮箱地址！");
            return false;
        }
        if($(this).hasClass('is-mobile') && !isMobile(v)){
            flag = false;
            $(this).addClass("error");
            alert("请填写11位的手机号码！");
            return false;
        }
    });
    if(!flag) return false;
    $("textarea.required", form).each(function(){
        var v = $.trim(this.value);
        if(v === '' || v === $(this).attr("placeholder")){
            flag = false;
            $(this).addClass("error");
            alert("请填写必填项！");
            return false;
        }
    });
//    $('select.require', form).each(function(){
//
//    });
    return flag;
}
function lockForm(form){
    $(":submit",form).addClass("wait").prop("disabled", true);
}
function unlockForm(form){
    $(":submit",form).removeClass("wait").prop("disabled", false);
}
/*屏幕滚动到.error的位置*/
function scrollToError() {
    var top = $(".error").eq(0).offset().top;
    top = top > 300 ? (top - 300) : top;
    $("html, body").animate({scrollTop: top}, 500);
}
(function(){
    function F(o){
        this.con = $(o);
    }
    F.prototype = {
        init : function(){
            this.t = this.con.attr("data-seconds") - 0;
            this.countdown();
        },
        countdown : function(){
            var _c = this,t = _c.t;
            if(t>0){
                setTimeout(function(){_c.countdown();},1000);
                var days = t > 86400 ? Math.floor(t/86400) : 0;
                var hrs = t > 3600 ? Math.floor(t%86400/3600) : 0;
                var mins = t > 60 ? Math.floor(t%86400%3600/60) : 0;
                var secs = t < 60 ? t : t%86400%3600%60;
                this.show(days,hrs,mins,secs);
                this.t--;
            }else{
                this.show(0,0,0,0);
            }
        },
        show : function(days,hrs,mins,secs){
            days = days > 99 ? days : (days > 9 ? "0"+days : "00"+days);
            hrs = hrs > 9 ? hrs : "0"+hrs;
            mins = mins > 9 ? mins : "0"+mins;
            secs = secs > 9 ? secs : "0"+secs;
            $(".js-day",this.con).text(days);
            $(".js-hr",this.con).text(hrs);
            $(".js-min",this.con).text(mins);
            $(".js-sec",this.con).text(secs);
        }
    };
    window.Countdown = F;
}());